<template>
	<div>
		<div class="crumbs">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>
					<i class="el-icon-lx-cascades"></i> 帮助中心
				</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="container">
			<div class="margin-top20 margin-bottom20">
				<el-button type="primary" icon="el-icon-lx-add" class="handle-del mr10" @click="handleEdit">
					新增
				</el-button>
				<el-button type="primary" icon="el-icon-search" @click="handleSearch">刷新</el-button>
			</div>
			<el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header"
				@selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55" align="center"></el-table-column>
				<el-table-column prop="sort" label="排序" width="100" align="center"></el-table-column>
				<el-table-column prop="title" label="标题" align="center"></el-table-column>
				<el-table-column prop="content" label="内容"></el-table-column>
				<el-table-column label="操作" align="center" width="200">
					<template slot-scope="scope">
						<el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑
						</el-button>
						<el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.$index, scope.row)">删除
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="margin-top10">
				<el-pagination background layout="->,total, prev, pager, next" :current-page="query.pageNumber"
					:page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
			</div>
		</div>

		<!-- 新增弹出框 -->
		<el-dialog :title="title" :visible.sync="editVisible" width="50%">
			<el-form :rules="rules" ref="form" :model="form" label-width="120px">
				<el-form-item label="标题" prop="title">
					<el-input v-model="form.title"></el-input>
				</el-form-item>
				<el-form-item label="内容" prop="content">
					<WangEditor v-model="form.content" width="90%"></WangEditor>
				</el-form-item>
				<el-form-item label="排序（小在前）" prop="sort">
					<el-input v-model="form.sort" type="number"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="editVisible = false">取 消</el-button>
				<el-button type="primary" @click="saveEdit('form')">确 定</el-button>
			</span>
		</el-dialog>

	</div>
</template>

<script>
	import WangEditor from '@/components/wangeditor'

	export default {
		name: 'categoryTable',
		components: {
			WangEditor
		},
		data() {
			return {
				query: {
					pageNumber: 1,
					pageSize: 30
				},
				title: '',
				tableData: [],
				delList: [],
				editVisible: false,
				pageTotal: 0,
				form: {},
				rules: {
					title: [{
						required: true,
						message: '请输入标题',
						trigger: 'blur'
					}],
					content: [{
						required: true,
						message: '请输入内容',
						trigger: 'blur'
					}],
					sort: [{
						required: true,
						message: '请输入排序',
						trigger: 'blur'
					}]
				}
			};
		},
		created() {
			this.getData();
		},
		methods: {
			//获取列表数据
			async getData() {
				const res = await this.$get('/admin/helpCenter/page', this.query)
				if (res.code == 1000) {
					this.tableData = res.data.list
					this.pageTotal = res.data.totalRow
				}
			},
			//新增编辑
			saveEdit(form) {
				this.$refs[form].validate((valid) => {
					if (valid) {
						this.$post('/admin/helpCenter/saveHelpCenter', this.form).then(res => {
							if (res.code == 1000) {
								this.$message.success('操作成功');
								this.getData();
								this.editVisible = false;
							}
						})
					} else {
						vm.$message.error('校验失败！');
					}
				});
			},
			// 触发搜索按钮
			handleSearch() {
				this.query.pageNumber = 1
				this.getData();
			},
			// 删除操作
			handleDelete(index, row) {
				this.$confirm('确定删除吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.$get('/admin/helpCenter/delete', {
						id: row.id
					}).then(res => {
						if (res.code == 1000) {
							this.$message.success('操作成功');
							this.getData();
						}
					})
				}).catch(() => {
					this.$message.success('处理失败！');
				});
			},
			// 编辑操作
			handleEdit(row) {
				if (row.id) {
					this.title = '编辑'
					this.form = JSON.parse(JSON.stringify(row))
				} else {
					this.title = '新增'
					this.form = {}
					this.form.content = ''
				}
				this.editVisible = true;
			},
			// 分页导航
			handlePageChange(val) {
				this.query.pageNumber = val
				this.getData();
			}
		}
	};
</script>

<style scoped>

</style>